let soundMgr = require('soundMgr');
let dragons = require('dragons');
let nodeMgr = require('nodeMgr');
let sounds = require('sounds');
let DragLayer = require('dragLayer');
let DragTips = require('DragTips');
let yySpine = require('yySpine');
let actions = require('actions');
let Level = require('Level');

cc.Class({
    extends: Level,

    properties: {
        feitime: 1,
        juli: 1000,
        xiaobu: cc.Node,
        dian: [cc.Node],
        bg1: cc.Node,
        bg2: cc.Node,
        bg3: cc.Node,
        guai: [cc.Node],
        guaiwuDot: cc.Node,
        ti: [cc.Node],
        zidanList: [cc.Prefab],
        bgMove1: cc.Node,
        bgMove2: cc.Node,
    },

    init() {
        this.width = cc.winSize.width;
        window.canMove = -1;
        this.round = 0;
        this.choose = 0;
        this.chooses = [5, 6, 7, 8, 10, 6, 9];
        this.speed = 300;
        this.isMove = false;
    },

    start() {

    },

    startLevel() {
        // nodeMgr.disableTouch();
        this.init();
        this.bgMove1.active = false;
        this.bgMove2.active = false;
        this._chongci();

    },

    _chongci() {
        var dian = this.dian;
        var i = 0;
        var xiaobu = this.xiaobu;


        var self = this;

        actions.moveTo(xiaobu, 0.4, 0, 0, () => {
            yySpine.playAnimation(xiaobu, 'walk', 0, true);
            xiaobu.children[0].active = true;
            actions.scaleTo(xiaobu, this._time(xiaobu, dian[i]), 0.5, 0.5, () => {
                // self.bgMove1.active = false;
                // self.bgMove2.active = false;
            });
            actions.moveTo(xiaobu, this._time(xiaobu, dian[i]), dian[i].x, dian[i].y, () => {
                i++;
                next();
            })

            actions.moveBy(cc.Camera.main.node, 3.28, 2456, 0, () => {

                self.bgMove1.x = 0;
                self.bgMove2.x = 1334;
            });
        })

        // this.bgMove1.active = false;
        // this.bgMove2.active = false;
        // self.bg1.active = true;
        // actions.moveTo(cc.Camera.main.node, 0.8, 400, 0);

        setTimeout(() => {
            this.bgMove1.active = false;
            this.bgMove2.active = false;
            self.bg1.active = true;
        }, 1000);

        function next() {
            actions.moveTo(xiaobu, self._time(dian[i - 1], dian[i]), dian[i].x, dian[i].y, () => {

                i++;
                if (i < 7) {
                    next();
                } else {
                    actions.moveBy(cc.Camera.main.node, 3.092, 1546, 0, () => {
                        self.bg1.active = false;
                        self.bgMove1.x = 0;
                        self.bgMove2.x = 1334;
                        self.bgMove1.active = true;
                        self.bgMove2.active = true;
                        self.isMove = true;
                        cc.Camera.main.node.x = 0;
                        self.xiaobu.x = -400;
                        self._next();
                        // xiaobu.stopAllActions();
                        // actions.moveBy(self.xiaobu, 1, 400, 0, () => {
                        //     self._next();
                        // });

                    });
                    actions.scaleTo(xiaobu, 2, 0.8, 0.8, () => {
                        yySpine.playAnimation(xiaobu, 'idle', 0, true);
                        xiaobu.children[0].active = false;
                    });
                    actions.moveBy(xiaobu, 3.092, 1146, 0, () => {

                    });
                    // actions.moveBy(cc.Camera.main.node, 1, 500, 0, () => {

                    // });
                }
            })
        }
    },

    chuguai() {
        var guai = this.guai[this.round];
        guai.active = true;
        actions.moveBy(guai, 0.5, 500, 0, () => {

        });
    },

    _time(star, end) {
        var pos = end.getPosition();
        let vec = pos.sub(star.getPosition());
        let distance = vec.mag();
        let time = distance / 1000;
        console.log('kanyixiashij', time);
        return time;
    },

    _next() {
        var guai = this.guai[this.round];
        guai.active = true;
        actions.moveTo(this.guai[this.round], 0.5, this.guaiwuDot.x, this.guaiwuDot.y, () => {
            this.tiban();
        });
    },

    tiban() {
        if (this.round == 0) {
            soundMgr.playSoundEffectByName("zshuxue13_01", () => {
                nodeMgr.enableTouch();
            });
        } else {
            soundMgr.playSoundEffectByName("zshuxue13_02");
            nodeMgr.enableTouch();
        }
        actions.moveTo(this.ti[this.round], 1, 60, 0);
    },

    onClick(data) {
        cc.audioEngine.pauseAll();
        this.nowTime = -1;
        var skin = 'tuo';
        var skin2 = 'tuo1'
        if (this.round == 0 || this.round == 2 || this.round == 4 || this.round == 6) {
            skin2 = 'tuo1';
            skin = 'tuo';
        } else {
            skin = 'xing1';
            skin2 = 'xing';
        }
        if (data.target.node.name == 'zidan') {
            data.target.node.name = 'zidan2'
            this.choose++;
            yySpine.setSpineSkin(data.target.node, skin);

        } else if (data.target.node.name == 'sure') {
            if (this.choose == this.chooses[this.round]) {
                soundMgr.playSoundEffectByName('tongyong_right', () => {
                });
                this.choose = 0;
                actions.moveBy(this.ti[this.round], 1, 0, 800, () => {
                    //开始打怪
                    window.canMove = this.round;
                    this.begin();
                    var guai = this.guai[this.round].children[0];
                    yySpine.playAnimation(guai, 'walk', 0, true);
                });
            } else {
                soundMgr.playSoundEffectByName('tongyong_error', () => {
                    soundMgr.playSoundEffectByName('zshuxue13_03');
                });
                this.choose = 0;
                for (var i = 2; i < this.ti[this.round].children.length; i++) {
                    yySpine.setSpineSkin(this.ti[this.round].children[i], skin2);
                }
            }
        } else if (data.target.node.name == 'zidan2') {
            data.target.node.name = 'zidan'
            this.choose--;
            var skin3 = 'tuo1'
            if (this.round == 0 || this.round == 2 || this.round == 4 || this.round == 6) {
                skin3 = 'tuo1';
            } else {
                skin3 = 'xing';
            }
            yySpine.setSpineSkin(data.target.node, skin3);
        }
    },

    begin() {
        var self = this;
        // yySpine.playAnimation(this.xiaobu, 'attack', 0, true);
        self.schedule(function () {

            soundMgr.playSoundEffectByName("biu");
            if (self.round == 0 || self.round == 2 || self.round == 4 || self.round == 6) {
                self.shengchengzidan1(0);
            } else {
                self.shengchengzidan1(1);
            }
            // switch (self.round) {
            //     case 0:
            //         // soundMgr.playSoundEffectByName("biu");
            //         self.shengchengzidan1(0);
            //         break;
            //     case 1:
            //         // soundMgr.playSoundEffectByName("biu");
            //         self.shengchengzidan1(1);
            //         break;
            // }
        }, 0.2);

    },

    shengchengzidan1(num) {
        let self = this;
        var item = cc.instantiate(self.zidanList[num]);
        self.node.addChild(item);
        item.x = self.xiaobu.x + 166;
        item.y = self.xiaobu.y - 12;
        item.active = true;
        item.scale = 0;
        actions.scaleTo(item, 0.5, 1, 1);
        item.name = 'zidan';
        actions.moveTo(item, this.feitime, this.juli, item.y, function () {
            item.active = false;
            item.destroy();
        });
    },

    update(dt) {

        // if (window.canMove == this.movetype) {
        //     if (self.node.x - 150 > self.feiJi.x) {
        //         self.node.x -= dt * this.speed;
        //     }
        //     if (self.node.y > self.feiJi.y - 50) {
        //         self.node.y -= dt * this.speed;
        //     } else if (self.node.y < self.feiJi.y) {
        //         self.node.y += dt * this.speed;
        //     }
        // }
        if (!this.isMove) return;
        this.bgMove1.x -= dt * 100;
        this.bgMove2.x -= dt * 100;
        if (this.bgMove1.x <= -1334) {
            this.bgMove1.x = 1334;
        }
        if (this.bgMove2.x <= -1334) {
            this.bgMove2.x = 1334;
        }


    },

    guai_death() {
        this.unscheduleAllCallbacks(this);
        this.round++;
        if (this.round == 7) {
            yySpine.playAnimation(this.xiaobu, 'happy', 0, true);
            soundMgr.playSoundEffectByName("tongyong_bigright", () => {
                this._gameOver();
            });
        } else {
            yySpine.playAnimation(this.xiaobu, 'happy', 0, true, () => {
                yySpine.playAnimation(this.xiaobu, 'idle', 0, true);
            });
            setTimeout(() => {
                yySpine.playAnimation(this.xiaobu, 'idle', 0, true);
                if (this.round == 2 || this.round == 4) {
                    this._chongci();
                } else {
                    this._next();
                }
            }, 2000);
        }


    }


});
